package com.thalesgroup.nl.trtdelft.dein.servlet.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;

/**
 * Handle the variable method calls on a Web service.
 * i.e. connect to the server-side of the web service and perform the action
 * 
 * @author Thales Research and Technology Delft
 */
public class Client {
    public static final int HTTP_OK = 200;
    private static final HttpClient httpClient = new DefaultHttpClient();
    private static final String contentType = "application/json"; //"application/json";
	
    /**
    * Do a GET action on the server
    **/
    public static String get(final String url, final String args) throws HttpException, IOException, URISyntaxException {
 
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000);
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpget);

        HttpEntity entity = response.getEntity();
	if(entity == null) { System.out.println("Output from Server ("+url+").... \n* NO CONTENT *"); return null; }
        InputStream instream = entity.getContent();
        String value = read(instream);
	System.out.println("Output from Server ("+url+").... \n" + value);
        return value;
    }
 
	/**
	 * Do a POST action on the server
	 **/
    public static String post(final String url, final String args) throws URISyntaxException, HttpException, IOException {
 
    //final HttpClient httpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000);
	System.out.println("Post on Server ("+url+").... \n" + args);
        
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", contentType);
        httpPost.addHeader("Accept", "application/json; charset=UTF-8");
//        HttpGet httpPost = new HttpGet(url);
        StringEntity entity = new StringEntity(args, "UTF-8");
        entity.setContentType(contentType);
        httpPost.setEntity(entity);
      //  httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost);
        InputStream instream = response.getEntity().getContent();
	String value = read(instream);
	System.out.println("Output from Server ("+url+").... \n" + value);
        return value;
    }
 
	/**
	 * Do a PUT action on the server
	 **/
    public static boolean put(final String url, final String args)
            throws URISyntaxException, HttpException, IOException {
    //  final HttpClient httpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000);
 
        HttpPut httpPut = new HttpPut(url);
        httpPut.addHeader("Accept", contentType);
        httpPut.addHeader("Content-Type", contentType);
        StringEntity entity = new StringEntity(args, "UTF-8");
        entity.setContentType(contentType);
        httpPut.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPut);
        int statusCode = response.getStatusLine().getStatusCode();
        return statusCode == HTTP_OK ? true : false;
    }
 
	/**
	 * Do a DELETE action on the server
	 **/
    public static boolean delete(final String url) throws HttpException,
            IOException, URISyntaxException {
    //        final HttpClient httpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000);
 
        HttpDelete httpDelete = new HttpDelete(url);
        httpDelete.addHeader("Accept",
                "text/html, image/jpeg, *; q=.2, */*; q=.2");
        HttpResponse response = httpClient.execute(httpDelete);
        int statusCode = response.getStatusLine().getStatusCode();
        return statusCode == HTTP_OK ? true : false;
    }
 
    private static String read(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in), 100);
        for (String line = r.readLine(); line != null; line = r.readLine()) {
            sb.append(line);
        }
        in.close();
        return sb.toString();
    }
}
